#include "VertexBufferGLES.h"
#include "RendererGLES.h"

namespace Nezha
{

	VertexBufferGLES::VertexBufferGLES(u32 vertexStride, u32 numVertex, HardwareBuffer::Usage usage, RendererGLES* r, float* data)
		:VertexBuffer(vertexStride, numVertex, usage, true)
		,mRenderer(r)
	{
		glGenBuffers(1, &mGLBufferId);
		GL_ERROR_DUMP(mRenderer->getAdaptor());

		mShadowBuffer.write((u8*)data, mSizeInBytes);
		mRenderer->bindGLBuffer(GL_ARRAY_BUFFER, mGLBufferId);

		glBufferData(GL_ARRAY_BUFFER, mSizeInBytes, (void*)data, GLenumMapping::BufferUsage(mUsage));
		GL_ERROR_DUMP(mRenderer->getAdaptor());
	}

	VertexBufferGLES::~VertexBufferGLES()
	{
		glDeleteBuffers(1, &mGLBufferId);
		GL_ERROR_DUMP(mRenderer->getAdaptor());
	}

	void* VertexBufferGLES::lockImpl(u32 offset, u32 length, LockOption opt)
	{
		NZ_ASSERT_EX(false, "Opengl es2 support no hardware buffer locking, please check the shadow buffer locking issue.");

		return NULL;
	}

	void VertexBufferGLES::unlockImpl()
	{
		// do nothing.
	}

	void VertexBufferGLES::syncShadowBuffer()
	{
		mRenderer->bindGLBuffer(GL_ARRAY_BUFFER, mGLBufferId);

		glBufferData(GL_ARRAY_BUFFER,mSizeInBytes, mShadowBuffer.data(), GLenumMapping::BufferUsage(mUsage));
		GL_ERROR_DUMP(mRenderer->getAdaptor());
	}

}//end namespace Nezha